/*  1:   */ package org.jetbrains.debugger;
/*  2:   */ 
/*  3:   */

import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.util.ActionCallback.Done;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.io.NettyUtil;
import org.jetbrains.jsonProtocol.Request;
import org.jetbrains.rpc.MessageHandler;
import org.jetbrains.rpc.MessageWriter;

/*  4:   */
/*  5:   */
/*  6:   */
/*  7:   */
/*  8:   */
/*  9:   */
/* 10:   */
/* 11:   */
/* 12:   */
/* 13:   */
/* 14:   */ 
/* 15:   */ public final class StandaloneVmHelper
/* 16:   */   extends MessageWriter
/* 17:   */ {
/* 18:   */   private volatile Channel a;
/* 19:   */   private final VmEx b;
/* 20:   */   
/* 21:   */   public StandaloneVmHelper(VmEx paramVmEx)
/* 22:   */   {
/* 23:20 */     this.b = paramVmEx;
/* 24:   */   }
/* 25:   */   
/* 26:   */   public boolean write(@NotNull CharSequence paramCharSequence)
/* 27:   */   {
/* 28:25 */     if (paramCharSequence == null) {
/* 29:25 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/debugger/StandaloneVmHelper", "write" }));
/* 30:   */     }
/* 31:25 */     return write(paramCharSequence);
/* 32:   */   }
/* 33:   */   
/* 34:   */   public boolean write(@NotNull Object paramObject)
/* 35:   */   {
/* 36:29 */     if (paramObject == null) {
/* 37:29 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/debugger/StandaloneVmHelper", "write" }));
/* 38:   */     }
/* 39:29 */     Channel localChannel = this.a;
/* 40:30 */     if ((localChannel == null) || (!localChannel.isActive())) {
/* 41:31 */       return false;
/* 42:   */     }
/* 43:33 */     localChannel.writeAndFlush(paramObject);
/* 44:34 */     return true;
/* 45:   */   }
/* 46:   */   
/* 47:   */   public void setChannel(@NotNull Channel paramChannel)
/* 48:   */   {
/* 49:45 */     if (paramChannel == null) {
/* 50:45 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/debugger/StandaloneVmHelper", "setChannel" }));
/* 51:   */     }
/* 52:45 */     this.a = paramChannel;
/* 53:46 */     paramChannel.closeFuture().addListener(new MyChannelFutureListener(null));
/* 54:   */   }
/* 55:   */   
/* 56:   */   private class MyChannelFutureListener
/* 57:   */     implements ChannelFutureListener
/* 58:   */   {
/* 59:   */     private MyChannelFutureListener() {}
/* 60:   */     
/* 61:   */     public void operationComplete(ChannelFuture paramChannelFuture)
/* 62:   */       throws Exception
/* 63:   */     {
/* 64:53 */       if (StandaloneVmHelper.this.a != null)
/* 65:   */       {
/* 66:54 */         StandaloneVmHelper.this.b.getCommandProcessor().closed();
/* 67:55 */         StandaloneVmHelper.this.b.getDebugListener().disconnected();
/* 68:   */       }
/* 69:   */     }
/* 70:   */   }
/* 71:   */   
/* 72:   */   public boolean isAttached()
/* 73:   */   {
/* 74:61 */     return this.a != null;
/* 75:   */   }
/* 76:   */   
/* 77:   */   public ActionCallback detach()
/* 78:   */   {
/* 79:65 */     final Channel localChannel = this.a;
/* 80:66 */     if (localChannel == null) {
/* 81:67 */       return new ActionCallback.Done();
/* 82:   */     }
/* 83:70 */     this.b.getCommandProcessor().cancelWaitingRequests();
/* 84:71 */     Request localRequest = this.b.createDisconnectRequest();
/* 85:73 */     if (localRequest == null)
/* 86:   */     {
/* 87:74 */       this.b.getCommandProcessor().closed();
/* 88:75 */       this.a = null;
/* 89:76 */       NettyUtil.closeAndReleaseFactory(localChannel);
/* 90:77 */       return ActionCallback.DONE;
/* 91:   */     }
/* 92:80 */     ActionCallback localActionCallback1 = this.b.getCommandProcessor().send(localRequest);
/* 93:81 */     this.b.getCommandProcessor().closed();
/* 94:82 */     this.a = null;
/* 95:83 */     final ActionCallback localActionCallback2 = new ActionCallback();
/* 96:84 */     localActionCallback1.doWhenProcessed(new Runnable()
/* 97:   */     {
/* 98:   */       public void run()
/* 99:   */       {
/* :0:   */         try
/* :1:   */         {
/* :2:88 */           StandaloneVmHelper.this.b.getCommandProcessor().cancelWaitingRequests();
/* :3:89 */           NettyUtil.closeAndReleaseFactory(localChannel);
/* :4:   */         }
/* :5:   */         finally
/* :6:   */         {
/* :7:92 */           localActionCallback2.setDone();
/* :8:   */         }
/* :9:   */       }
/* ;0:95 */     });
/* ;1:96 */     return localActionCallback2;
/* ;2:   */   }
/* ;3:   */   
/* ;4:   */   public static abstract interface VmEx
/* ;5:   */     extends Vm
/* ;6:   */   {
/* ;7:   */     public abstract MessageHandler<?, ?, ?, ?> getCommandProcessor();
/* ;8:   */     
/* ;9:   */     @Nullable
/* <0:   */     public abstract Request createDisconnectRequest();
/* <1:   */   }
/* <2:   */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.debugger.StandaloneVmHelper

 * JD-Core Version:    0.7.0.1

 */